package com.health.pay.controller;

import com.health.pay.service.AliOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by fangjicai on 2020/3/30.
 */
@Controller
@Slf4j
public class AliPayTradeController {

    @Autowired
    private AliOrderService aliOrderService;

//    @Autowired
//    private OrderSergvice orderSergvice;

    @ResponseBody
    @RequestMapping("/orderId/{id}")
    public String createPayHtml(@PathVariable("id") Long orderId){
        //this.orderSergvice.findById(orderId)--->order对象
        //Long total_money=order.getTotalMoney() 获取金额
        Long total_money=100L;
        //String name=order.getName()--获取订单名称
        String name="huaweipro40";
        //String desc=order.getDesc()
        String desc="xxx";
        String result = this.aliOrderService.createOrder(orderId, total_money, name, desc);
        System.out.println(result);
        return result;
    }


    /**
     * 对接的支付用户
     * 支付宝支付完成后同步回调地址，用于向用户通过浏览器同步通知支付结果（一般开发中仅仅作为通知，不会进行订单修改
     * 操作---因为如果进行订单等相关操作，可能导致用户获取反馈的时间变长，导致用户体验不好）
     * @param request
     * @return
     */
    @GetMapping("/returnUrl")
    public String returnUrl(HttpServletRequest request){
        String result;
        try {
          result= this.aliOrderService.returnUrlCheck(request);
        } catch (Exception e) {
            e.printStackTrace();
            result=e.getMessage();
        }
        return result;
    }

    /**
     * 支付宝异步调用该接口，主要对接上家平台，完成订单交易状态的异步通知，后台修改订单状态，并返回支付宝状态
     * success  fail
     * @param request
     * @return
     */
    @PostMapping("/notityUrl")
    public String notityUrl(HttpServletRequest request){
        String flag="fail"; //success fail
        try {
          flag=  this.aliOrderService.notityUrlCheck(request);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("支付异步回调出现异常:{}",e.getMessage());
        }
        return flag;
    }

    /**
     * 订单状态查询
     * @param tradeNo 订单ID
     * @param outTradeNo  支付宝交易ID
     * @return
     */
    @GetMapping("/queryTradeStatus")
    public String queryTradeStatus(String tradeNo,String outTradeNo){
        String result;
        try {
         result=  this.aliOrderService.queryTradeStatus(tradeNo,outTradeNo);
        } catch (Exception e) {
            e.printStackTrace();
            result=e.getMessage();
        }
        return result;
    }







}
